package com.wmcom.wm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.wmcom.wm.common.BaseContext;
import com.wmcom.wm.common.R;
import com.wmcom.wm.entity.ShoppingCart;
import com.wmcom.wm.service.DishService;
import com.wmcom.wm.service.SetmealService;
import com.wmcom.wm.service.ShoppingCartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;
    @Resource
    private DishService dishService;
    @Resource
    private SetmealService setmealService;

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session) {
        long userid= (long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userid);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 添加菜品或者套餐到购物车
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        /**
         * 设置用户id
         */
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断添加的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        //    查询是否已经购买过该菜品或者套餐
        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);

        } else {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(wrapper);

        //判断是否已经添加过该菜品或者套餐，如果添加过，则数量家1
        if (cartServiceOne != null) {
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
            //Dish dish=dishService.getById(cartServiceOne.getDishId());
            //Setmeal setmeal=setmealService.getById(cartServiceOne.getSetmealId());
            //价格
            //double bigDecimal = cartServiceOne.getAmount().doubleValue();
            //double bigDecimal2=0.00;
            //double bigDecimal1=0.00;
            // if (dish!=null){
            //      bigDecimal1= dish.getPrice().doubleValue();
            //      bigDecimal1=bigDecimal1/100.00;
            //      bigDecimal2=bigDecimal+bigDecimal1;
            // }else {
            //      bigDecimal1= setmeal.getPrice().doubleValue();
            //     bigDecimal1=bigDecimal1/100.00;
            //     bigDecimal2=bigDecimal+bigDecimal1;
            // }
            //cartServiceOne.setAmount(BigDecimal.valueOf(bigDecimal2));
            shoppingCartService.updateById(cartServiceOne);
            //cartServiceOne.setAmount(BigDecimal.valueOf(bigDecimal1));
        } else {
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 减少菜品或者套餐到购物车
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingone = shoppingCartService.getOne(wrapper);
        //Dish dish=dishService.getById(shoppingone.getDishId());
        //Setmeal setmeal=setmealService.getById(shoppingone.getSetmealId());
        //价格
        //double bigDecimal = shoppingone.getAmount().doubleValue();
        //double bigDecimal2=0.00;
        //double bigDecimal1=0.00;
        if (shoppingone.getNumber() > 1) {
            shoppingone.setNumber(shoppingone.getNumber() - 1);
            //if (dish!=null){
            //    bigDecimal1= dish.getPrice().doubleValue();
            //    bigDecimal1=bigDecimal1/100.00;
            //    bigDecimal2=bigDecimal-bigDecimal1;
            //}else {
            //    bigDecimal1= setmeal.getPrice().doubleValue();
            //    bigDecimal1=bigDecimal1/100.00;
            //    bigDecimal2=bigDecimal-bigDecimal1;
            //}
            //shoppingone.setAmount(BigDecimal.valueOf(bigDecimal2));
            shoppingCartService.updateById(shoppingone);
            //shoppingone.setAmount(BigDecimal.valueOf(bigDecimal1));
        } else {
            shoppingCartService.removeById(shoppingone);
            shoppingone=shoppingCart;
        }
        return R.success(shoppingone);
    }
    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String>cancel(){
        //清空该用户购物车的所有商品
        LambdaQueryWrapper<ShoppingCart>wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("ok");
    }
}